/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.harvard.s3xyback;

import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;

/**
 * FXML Controller class
 *
 * @author ayoung
 */
public class FXMLController implements Initializable {

    @FXML
    Label errorMessage;
    @FXML
    ProgressIndicator pin;
    @FXML
    Button synchronize;
    @FXML
    Button reconfigure;
    
    private Main application;
    private ExecutorService threadExecutor;
    private WatchDir watchdir;
    private boolean isRunning;
   
    public void setApp(Main app){
        application = app;
    }
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        pin.setVisible(false);
        pin.setProgress(-1.0);
        isRunning = false;
        threadExecutor = Executors.newSingleThreadExecutor();
        try {
            S3xyBack s3xy = new S3xyBack();
            ArrayList<Path> dirPaths = new ArrayList<Path>();
            String[] dirs = s3xy.getDir();
            for (String s : dirs) {
                dirPaths.add(Paths.get(s));
            }
            boolean recursive = true;
            watchdir = new WatchDir(dirPaths, recursive, s3xy);
        }
        catch (Exception e) 
        {
            System.out.println(e);
        }
    }    
    
    @FXML
    private void sync(ActionEvent event) throws Exception {
        if (!isRunning) {
            synchronize.setText("Cancel");
            pin.setVisible(true);
            errorMessage.setVisible(false);
            isRunning = true;
            try {
                // Watch directories and synchronize changes with S3. Record to log file.
                Task<Void> task = new Task<Void>() {
                    @Override protected Void call() throws Exception {
                        watchdir.setCancel(false);
                        watchdir.synchronize();
                        return null;
                    }
                };
            threadExecutor.submit(task);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        else {
            synchronize.setText("Synchronize");
            watchdir.setCancel(true);
            errorMessage.setVisible(true);
            errorMessage.setText("Synchronization has been canceled");
            pin.setVisible(false);
            isRunning = false;
        }
    }
    
    @FXML
    private void reconfigure(ActionEvent event) {
        application.gotoConfig();
    }
}
